package com.gjy.disruptor.uuid.newd.dis;

import com.fasterxml.uuid.impl.RandomBasedGenerator;
import com.lmax.disruptor.EventTranslatorOneArg;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 事件转换器，将Netty上下文和UUID生成器放入事件中。
 *
 * @author gjy
 * @version 1.0
 * @since 2025-10-11 10:21:07
 */
public class UuidEventTranslator implements EventTranslatorOneArg<UuidEvent, Object[]> {
    private static final Logger log = LoggerFactory.getLogger(UuidEventTranslator.class);
    public static final UuidEventTranslator INSTANCE = new UuidEventTranslator();

    private UuidEventTranslator() {
    }

    @Override
    public void translateTo(UuidEvent event, long sequence, Object[] args) {
        RandomBasedGenerator generator = (RandomBasedGenerator) args[0];
        ChannelHandlerContext ctx = (ChannelHandlerContext) args[1];
        Long requestId = (Long) args[2];

        String uuid = generator.generate().toString();
        log.info("处理的UUID: {}", uuid);
        event.setUuid(uuid);
        event.setCtx(ctx);
        event.setRequestId(requestId != null ? requestId : 0);
    }

}
